﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Work.BL.Managers;
using Work.BL.POCOs;
using Work.DAL.Hibernate.Abstract;
using Work.DAL.Hibernate.Entities;
using Work.Tools.ObjectMapper;

namespace Work.BL.Hibernate.Managers
{
    public class HProfileEmployeeManager : IProfileEmployeeManager
    {
        #region Fields

        private readonly ICWSessionFactory _sessionFactory;
        private readonly IObjectMapper _objectMapper;

        #endregion

        #region Constructors

        public HProfileEmployeeManager(ICWSessionFactory sessionFactory, IObjectMapper objectMapper)
        {
            this._sessionFactory = sessionFactory;
            this._objectMapper = objectMapper;
        }

        #endregion

        #region IProfileEmployee Members

        public ProfileEmployeePOCO Insert(ProfileEmployeePOCO employeePoco)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                ProfileEmployee employee = _objectMapper.Map<ProfileEmployeePOCO, ProfileEmployee>(employeePoco);
                session.Save(employee);
                session.Flush();
                return _objectMapper.Map<ProfileEmployee, ProfileEmployeePOCO>(employee);
            }
        }

        public List<ProfileEmployeePOCO> GetEmloyees()
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                IList<ProfileEmployee> users = session.CreateCriteria<ProfileEmployee>().List<ProfileEmployee>();
                return users
                    .Select(r => this._objectMapper.Map<ProfileEmployee, ProfileEmployeePOCO>(r))
                    .ToList();
            }
        }

        public List<ProfileEmployeePOCO> GetEmloyees(int startIndex, int maxCount)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                IList<ProfileEmployee> projects = session.CreateCriteria<ProfileEmployee>()
                    .SetFirstResult(startIndex)
                    .SetMaxResults(maxCount)
                    .List<ProfileEmployee>();
                return projects
                    .Select(r => this._objectMapper.Map<ProfileEmployee, ProfileEmployeePOCO>(r))
                    .ToList();
            }
        }

        public int GetEmployeeCount()
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                int rowCount = session.CreateCriteria<ProfileEmployee>()
                    .SetProjection(Projections.RowCount()).UniqueResult<int>();
                return rowCount;
            }
        }

        public ProfileEmployeePOCO GetEmployeeById(int id)
        {
                using (ISession session = this._sessionFactory.CreateSession())
                {
                    ProfileEmployee profileEmployee = session.Get<ProfileEmployee>(id);
                    return _objectMapper.Map<ProfileEmployee, ProfileEmployeePOCO>(profileEmployee);
                }
            
        }

        public ProfileEmployeePOCO Edit(ProfileEmployeePOCO employeePoco)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                ProfileEmployee profileEmployee = _objectMapper.Map<ProfileEmployeePOCO, ProfileEmployee>(employeePoco);
                session.Update(profileEmployee);
                session.Flush();
                return _objectMapper.Map<ProfileEmployee, ProfileEmployeePOCO>(profileEmployee);
            }
        }

        public void Delete(ProfileEmployeePOCO profileEmployee)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                ProfileEmployee region = _objectMapper.Map<ProfileEmployeePOCO, ProfileEmployee>(profileEmployee);
                session.Delete(region);
                session.Flush();
            }
        }

        #endregion
    }
}
